/*Copyright (C) 2013-2021 LZE*/
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
struct node{
	ll step;
	ll number;
}a[1000110];
deque<node>Q;
ll maxn[1000110]={0};
ll minn[1000110]={0};
ll n,k,maxnn=0,minnn=0;
int main(){
	cin>>n>>k;
	for(int i=1;i<=n;i++){
		cin>>a[i].number;
		a[i].step=i;
	}
	ll t=0;
	for(int i=1;i<=n;i++){
		while(Q.size()&&Q.back().number>a[i].number){
			Q.pop_back();
		}
		while(Q.size()&&Q.front().step<i-k+1)Q.pop_front();
		Q.push_back(a[i]);
		minn[++minnn]=Q.front().number;
	}
	Q.clear();
	for(int i=1;i<=n;i++){
		while(Q.size()&&Q.back().number<a[i].number){
			Q.pop_back();
		}
		while(Q.size()&&Q.front().step<i-k+1)Q.pop_front();
		Q.push_back(a[i]);
		maxn[++maxnn]=Q.front().number;
	}
	for(int i=k;i<=maxnn;i++){
		cout<<minn[i]<<" ";
	}
	cout<<endl;
	for(int i=k;i<=maxnn;i++){
		cout<<maxn[i]<<" ";
	}
	return 0;
}
/*
8 3
1 3 -1 -3 5 3 6 7
*/